package god.day02;



/**
 * @author wang hao
 * @date 2023-02-18 17:51
 *
 *     数组实现队列
 */
public class Demo01 {


    public static class MyQueue{
        private int[] arr;
        private int pushi;
        private int polli;
        private int size;
        private final int limit;

        public MyQueue(int limit){
            arr = new int[limit];
            pushi = 0;
            polli = 0;
            size = 0;
            this.limit = limit;
        }

        public void push(int value){
            if (size == limit){
                throw new RuntimeException("栈满了，不能在加了");
            }
            size++;
            arr[pushi] = value;
            pushi = nextIndex(pushi);
        }

        public int pop(){
            if (size == 0){
                throw new RuntimeException("栈空了，不能在拿了");
            }
            size--;
            int ans = arr[polli];
            polli = nextIndex(pushi);
            return ans;
        }

        public boolean isEmpty(){
            return size == 0;
        }

        public int nextIndex(int index) {
            return index < limit - 1 ? index + 1 : 0;
        }


    }

}
